package com.koron.order.risk.riskDuty.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.risk.riskDuty.bean.dto.LineRiskDutyPathDTO;
import com.koron.order.risk.riskDuty.bean.po.LineRiskDuty;
import com.koron.order.risk.riskDuty.bean.po.LineRiskDutyPath;
import com.koron.order.risk.riskDuty.bean.query.LineRiskDutyPathQuery;
import com.koron.order.risk.riskDuty.mapper.LineRiskDutyPathMapper;
import com.koron.order.risk.riskDuty.service.LineRiskDutyPathService;
import com.koron.order.risk.riskDuty.service.LineRiskDutyPositionService;
import com.koron.order.risk.riskDuty.service.LineRiskDutyService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 看护值守巡视路线信息(LineRiskDutyPath)表服务实现类
 *
 * @author jzp
 * @since 2022-12-28 17:03:12
 */
@Service
public class LineRiskDutyPathServiceImpl implements LineRiskDutyPathService {

    @Autowired
    private LineRiskDutyPositionService lineRiskDutyPositionService;

    @Autowired
    private LineRiskDutyService lineRiskDutyService;

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LineRiskDutyPath getById(SessionFactory factory, String id) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public LineRiskDutyPathDTO getDtoById(SessionFactory factory, String id) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.selectDtoById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LineRiskDutyPath> list(SessionFactory factory, LineRiskDutyPathQuery query) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineRiskDutyPath> page(SessionFactory factory, PageQuery pageQuery, LineRiskDutyPathQuery query) {
        return LineRiskDutyPathService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineRiskDutyPath lineRiskDutyPath) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.insertOrUpdate(lineRiskDutyPath) > 0;
    }

    @Override
    @TaskAnnotation("saveDto")
    public LineRiskDutyPathDTO saveDto(SessionFactory factory, LineRiskDutyPathDTO lineRiskDutyPathDto) {
        // 1. 保存基本信息
        save(factory, lineRiskDutyPathDto);

        // 2. 保存点信息
        lineRiskDutyPathDto.getLineRiskDutyPositionList().forEach(e -> e.setPathId(lineRiskDutyPathDto.getId()));
        lineRiskDutyPositionService.removeByPathId(factory, lineRiskDutyPathDto.getId());
        lineRiskDutyPositionService.saveBatch(factory, lineRiskDutyPathDto.getLineRiskDutyPositionList());

        // 3. 看护值守单变成值守中
        checkAndUpdateInDuty(factory, lineRiskDutyPathDto.getRiskDutyId());
        return lineRiskDutyPathDto;
    }

    private void checkAndUpdateInDuty(SessionFactory factory, String dutyId) {
        LineRiskDuty lineRiskDuty = lineRiskDutyService.getById(factory, dutyId);
        if ("1".equals(lineRiskDuty.getStatus())) {
            lineRiskDuty.setStatus("2");
            lineRiskDutyService.measureSetImplemented(factory, lineRiskDuty);
            lineRiskDutyService.updateStatus(factory, dutyId, "2");
        }
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineRiskDutyPath lineRiskDutyPath) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.update(lineRiskDutyPath) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineRiskDutyPathMapper mapper = factory.getMapper(LineRiskDutyPathMapper.class);
        return mapper.hidden(id) > 0;
    }
}
